package com.gouaixin.bee.beeframework2.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

import com.gouaixin.bee.beeframework2.MyApplication;
import com.gouaixin.bee.beeframework2.R;
import com.gouaixin.bee.beeframework2.component.FloatView;
import com.gouaixin.bee.beeframework2.extra.floatView.FloaView_MainActivity;
import com.gouaixin.bee.beeframework2.fragment.daojishi_fragment_huafei;
import com.gouaixin.bee.beeframework2.tool.PreferencesUtils;

/**
 * Created by Administrator on 2015/4/27.
 */
//充值成功，跳转到的送话费页面
public class paySuccessActivity extends FragmentActivity {
    Context context = paySuccessActivity.this;


    private WindowManager wm = null;
    private WindowManager.LayoutParams wmParams = null;

    public static FloatView myFV = null;
    int payAmount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.paysuccess);

        initIntent();

        initBanner();

//
//        createView();
//
//        initPayAmount();
    }

    private void initBanner() {
        PreferencesUtils.putInt(context, "payAmount", payAmount);
//        PreferencesUtils.putLong(context,"bannerTime",System.currentTimeMillis()+ MyConfig.Push);
        Intent k1 = new Intent(context, FloaView_MainActivity.class);
        context.startActivity(k1);
    }

    private void initIntent() {
        Intent it = getIntent();
        payAmount = it.getIntExtra("amount", 0);
        PreferencesUtils.putInt(context, "payAmount", payAmount);

    }

    private void initPayAmount() {
        payAmount = PreferencesUtils.getInt(context, "payAmount", 0);
        Log.e("test", "paySuccessActivity:获取的已充值金额=" + payAmount);
        int giftAmount = 2 * payAmount;
        String str = getResources().getString(R.string.chonghuafei);
        str = str.replace("XXX", payAmount + "");
        str = str.replace("YYY", giftAmount + "");
        Log.e("test", "paySuccessActivity:str=" + str);

//        TextView tv = (TextView) findViewById(R.id.tv);

        if (payAmount != 0) {
//            tv.setText(str);
            myFV.setText(str);
            myFV.setBackgroundResource(R.drawable.corner_view_blue);
        }


    }


    private void createView() {
//        View view = (FloatView) LayoutInflater.from(context).inflate(R.layout.floatview, null);
        myFV = new FloatView(context);
        Log.e("test", "myFV.getHeight()=" + myFV.getHeight());

        //获取WindowManager
        wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        //设置LayoutParams(全局变量）相关参数
        wmParams = ((MyApplication) getApplication()).getMywmParams();

        /**
         *以下都是WindowManager.LayoutParams的相关属性
         * 具体用途可参考SDK文档
         */
        wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;   //设置window type
        wmParams.format = PixelFormat.RGBA_8888;   //设置图片格式，效果为背景透明

        //设置Window flag
        wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        /*
         * 下面的flags属性的效果形同“锁定”。
         * 悬浮窗不可触摸，不接受任何事件,同时不影响后面的事件响应。
         wmParams.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL
                               | LayoutParams.FLAG_NOT_FOCUSABLE
                               | LayoutParams.FLAG_NOT_TOUCHABLE;
        */


        wmParams.gravity = Gravity.LEFT | Gravity.TOP;   //调整悬浮窗口至左上角
        //以屏幕左上角为原点，设置x、y初始值
        wmParams.x = 0;
        wmParams.y = 0;

        //设置悬浮窗口长宽数据
        wmParams.width = wm.getDefaultDisplay().getWidth();

        wmParams.height = 120;




        myFV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Log.e("test", "你目前不在pay1，可以点击");
                startActivity(new Intent(context, paySuccessActivity2.class));
                finish();


            }
        });

        daojishi_fragment_huafei fragment = (daojishi_fragment_huafei) getSupportFragmentManager().findFragmentById(R.id.daojishi_fragment);


//        //延时30分钟后，移除fv
        long tomorrowData = PreferencesUtils.getLong(context, "tomorrowData", 0);
        long temp = tomorrowData - System.currentTimeMillis();
        Log.e("test", "剩余的tomorrowData=" + temp);

        //显示myFloatView图像
//        boolean haveAddFv = PreferencesUtils.getBoolean(context, "haveAddFv", false);
//        if (haveAddFv == false) {
//
//            PreferencesUtils.getBoolean(context, "haveAddFv", true);
//            Log.e("test", "haveAddFv:" + "已经添加");
        wm.addView(myFV, wmParams);
        //到截至时间移除
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                wm.removeView(myFV);
            }
        }, temp);
//        }


    }


}
